package net.kazed.nextaction.task;

import java.util.ArrayList;
import java.util.List;

import net.kazed.android.inject.Component;
import net.kazed.nextaction.R;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

@Component(TaskAttachmentManager.class)
public class TaskAttachmentManagerImpl implements TaskAttachmentManager {
    
    private List<AttachmentType> attachmentTypeList;
    private List<AttachmentType> activeAttachmentTypeList;
    
    /**
     * Constructor.
     */
    public TaskAttachmentManagerImpl() {
        super();
        attachmentTypeList = new ArrayList<AttachmentType>();
        attachmentTypeList.add(new BarCodeAttachmentType(R.string.attachment_type_barcode,
                        R.string.attachment_type_barcode));
        attachmentTypeList.add(new ContactAttachmentType(R.string.attachment_type_contact,
                        R.string.attachment_type_contact));
    }

    /**
     * {@inheritDoc}
     */
    public List<AttachmentType> getAttachmentTypes() {
        return attachmentTypeList;
    }

    /**
     * {@inheritDoc}
     */
    public ListAdapter getAttachmentTypeListAdapter(Context context, int itemResource) {
        PackageManager packageManager = context.getPackageManager();
        Resources resources = context.getResources();
        activeAttachmentTypeList = new ArrayList<AttachmentType>();
        List<String> attachmentTypeLabels = new ArrayList<String>(attachmentTypeList.size());
        
        for (AttachmentType attachmentType : attachmentTypeList) {
            if (attachmentType.isIntentTargetAvailable(packageManager)) {
                activeAttachmentTypeList.add(attachmentType);
                attachmentTypeLabels.add(resources.getString(attachmentType.getLabelResource()));
            }
        }
        
        ListAdapter adapter = new ArrayAdapter<String>(context, itemResource,
                        attachmentTypeLabels);
        return adapter;
    }

    /**
     * {@inheritDoc}
     */
    public AttachmentType getAttachmentType(int index) {
        return attachmentTypeList.get(index);
    }

    /**
     * {@inheritDoc}
     */
    public AttachmentType getActiveAttachmentType(int index) {
        return activeAttachmentTypeList.get(index);
    }
    
    /**
     * {@inheritDoc}
     */
    public AttachmentType getAttachmentTypeByRequestCode(int requestCode) {
        AttachmentType foundAttachmentType = null;
        for (AttachmentType attachmentType : attachmentTypeList) {
            if (attachmentType.getRequestCode() == requestCode) {
                foundAttachmentType = attachmentType;
            }
        }        
        return foundAttachmentType;
    }

}
